package com.ogawa.chair7808.ui.activity

import android.view.View
import android.widget.TextView
import com.ogawa.base.Constant.CommmandNum
import com.ogawa.chair7808.R
import com.ogawa.chair7808.cache.deviceStatusBean
import com.ogawa.chair7808.manager.DataManager.Companion.dataManager
import kotlinx.android.synthetic.main.activity_attitude_adjustment_7808.*
import java.util.*

/**
 * 姿态调节
 */
class AttitudeAdjustmentActivity7808 : BaseMqtt7808Activity(), View.OnClickListener //        , IAttitudeAdjustmentView7506
{
    private val mIvAdjustViews: MutableList<TextView?> = ArrayList()
    private var mZero: String? = null

    override fun setLayoutId(): Int {
        return R.layout.activity_attitude_adjustment_7808
    }

    override fun initTitleBar() {
        super.initTitleBar()
        titleBar.setGreenBackBotton()
        titleBar.setTitle("姿态调节")
        setBackGround(R.mipmap.bg)
    }

    override fun initView(layoutId: Int) {
        super.initView(layoutId)
        initView()
//        LiveEventBus.get(LIVE_EVENT_BUS_KEY_END_TIME, Int::class.java).observe(this, {
//            if (CommonUtil.isActivityTop(AttitudeAdjustmentActivity7808::class.java, this@AttitudeAdjustmentActivity7808)) {
//                showFinishDialog(this@AttitudeAdjustmentActivity7808, "0") {
//                    cancleFinishDialog()
//                    ActivityUtils.finishToActivity(Home7808Activity7808::class.java,false)
////                    Activity7808Utils.startHomeActivityClearTop(this@AttitudeAdjustmentActivity7808)
////                    finish()
//                }
//            }
//        })
        updateView()
    }

    private fun initView() {
        mIvAdjustViews.add(chair_lift_leg)
        mIvAdjustViews.add(chair_lower_leg)
        mIvAdjustViews.add(cahir_extension_leg)
        mIvAdjustViews.add(chair_shrink_legs)
        mIvAdjustViews.add(chairdown)
        mIvAdjustViews.add(chairup)

        ll_reset.setOnClickListener(this)
        ll_zero1.setOnClickListener(this)
        ll_zero2.setOnClickListener(this)
        chairup.setOnClickListener(this)
        chairdown.setOnClickListener(this)
        chair_shrink_legs.setOnClickListener(this)
        cahir_extension_leg.setOnClickListener(this)
        chair_lower_leg.setOnClickListener(this)
        chair_lift_leg.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        val i = v.id
        if (i == R.id.chairup) {
            dataManager.sendCommond(CommmandNum.SHENBEI, "1")
        } else if (i == R.id.chairdown) {
            dataManager.sendCommond(CommmandNum.DAOBEI, "0")
        } else if (i == R.id.chair_shrink_legs) {
            dataManager.sendCommond(CommmandNum.SUOTUI, "0")
        } else if (i == R.id.chair_lower_leg) {
            dataManager.sendCommond(CommmandNum.JIANGTUI, "0")
        } else if (i == R.id.chair_lift_leg) {
            dataManager.sendCommond(CommmandNum.TAITUI, "1")
        } else if (i == R.id.cahir_extension_leg) {
            dataManager.sendCommond(CommmandNum.SHENTUI, "1")
        } else if (i == R.id.ll_reset) {
            dataManager.sendCommond(CommmandNum.FUWEI, "0")
        } else if (i == R.id.ll_zero1) {
            dataManager.sendCommond(CommmandNum.LINZL1, "1")
        } else if (i == R.id.ll_zero2) {
            dataManager.sendCommond(CommmandNum.LINZL2, "2")
        }
    }



    fun updateView() {
        mZero = deviceStatusBean.zeroGravity
        iv_zero1.isSelected = false
        iv_zero2.isSelected = false
        iv_reset.isSelected = false
        if (mZero == "0") {
        } else if (mZero == "1") {
            iv_zero1.isSelected = true
        } else if (mZero == "2") {
            iv_zero2.isSelected = true
        }else if (mZero == "5"){
            iv_reset.isSelected = true
        }




        val LegPosAdjust = deviceStatusBean.legPosAdjust
        if (LegPosAdjust == "1") {
            chair_lift_leg.isSelected = true
            chair_lower_leg.isSelected = false
        } else if (LegPosAdjust == "0") {
            chair_lift_leg.isSelected = false
            chair_lower_leg.isSelected = true
        } else {
            chair_lift_leg.isSelected = false
            chair_lower_leg.isSelected = false
        }
        val legPosAdjustX = deviceStatusBean.legPosAdjustX
        cahir_extension_leg.isSelected = false
        chair_shrink_legs.isSelected = false
        if (legPosAdjustX == "1") {
            cahir_extension_leg.isSelected = true
            chair_shrink_legs.isSelected = false
        } else if (legPosAdjustX == "0") {
            cahir_extension_leg.isSelected = false
            chair_shrink_legs.isSelected = true
        }
        val backPosAdjust = deviceStatusBean.backPosAdjust
        chairdown.isSelected = false
        chairup.isSelected = false
        if (backPosAdjust == "0") {
            chairdown.isSelected = true
            chairup.isSelected = false
        } else if (backPosAdjust == "1") {
            chairdown.isSelected = false
            chairup.isSelected = true
        }
    }
    public override fun changeViewByMqttMessage() {
        super.changeViewByMqttMessage()
        updateView()
    }
}